/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package maincz.tests.webapp.backingbeans;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.SessionScoped;
import maincz.tests.webapp.entity.Cache;
import maincz.tests.webapp.service.ICachesService;
import maincz.tests.webapp.utils.CacheUtil;
import maincz.tests.webapp.utils.NewCacheProcessor;
import maincz.tests.webapp.utils.PageCrawler;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 * @author Main
 */
@Component("testBean")
@SessionScoped
public class TestBean implements NewCacheProcessor {
    private static final String TEST_PAGE = "http://www.geocaching.com/seek/nearest.aspx?country_id=56&as=1&ex=1&cFilter=9a79e6ce-3344-409c-bbe9-496530baf758&children=n";
            
    private static final Logger log = Logger.getLogger("MyLogger");
    
    @Autowired
    private ICachesService cachesService;
    
    public List<Cache> caches = new ArrayList<Cache>();

    public List<Cache> getCaches() {
        return caches;
    }

    public void setCaches(List<Cache> caches) {
        this.caches = caches;
    }
    
    
    public void runTest() {
        
        log.info("Start test method");
        this.caches.clear();        
        
        Connection conn = Jsoup.connect(TEST_PAGE);
        try {
            Document doc = conn.userAgent("Mozilla").get();
            Map<String, String> cookies = conn.response().cookies();
                        
            Integer pageCount = PageCrawler.getPageCount(doc);
            log.info("Pocet stran: " + pageCount);            
            Integer pageNumber = PageCrawler.getPageNumber(doc);
            log.info("Strana: " + pageNumber);  
            
            CacheUtil.parseSearchPage(doc, this);
            
            while (true) {
                
                doc = PageCrawler.getNextPage(TEST_PAGE, doc, cookies);
                pageNumber = PageCrawler.getPageNumber(doc);
                log.info("Strana: " + String.valueOf(pageNumber));
                CacheUtil.parseSearchPage(doc, this);
                
                if (PageCrawler.getPageNumber(doc) == pageCount) {
                    break;
                }
            }    
        } catch (IOException ex) {
            Logger.getLogger(TestBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        
            
        log.info("End test method");
    }

    @Override
    public void processCache(Cache newCache) {
        // this.caches.add(newCache);
        if (newCache != null && newCache.getGeocode() != null) {
            this.cachesService.addOrUpdateCache(newCache);
            log.info("Cache processed - " + newCache.getGeocode());        
        }
        else {
            log.info("Skipped (probably premium cache)");        
        }
        
    }
        
}
